<?php
	session_start();
	if(isset($_REQUEST['mask']))
	{
		$mask=$_REQUEST['mask'];
		require("../DAO/DataHelper.php");
		require("../DAO/BinhLuanSuKienDTO.php");
		require("../DAO/BinhLuanSuKienDAO.php");
		if(isset($_REQUEST['mask'])&& isset($_REQUEST['nd']))
		{
			$mask=$_REQUEST['mask'];
			$nd=$_REQUEST['nd'];
			if(isset($_SESSION['IsLogin'])&& isset($_SESSION['UserName']))
			{
				$username=$_SESSION['UserName'];
				$sk=new BinhLuanSuKienDTO();
				$sk->blsk_ma_su_kien=$mask;
				$sk->blsk_username=$username;
				$sk->blsk_noi_dung=$nd;
				$sk->blsk_ngay_tao=date("Y/m/d");
				$sk->blsk_tong_so_sao=0;
				
				BinhLuanSuKienDAO::ThemBinhLuanSuKien($sk);
			}
		}
		$maxItems=BinhLuanSuKienDAO::DemSoBinhLuan($mask);
		
		$rowPerPage=4;
		$maxPages=ceil($maxItems/$rowPerPage);
		$curPage=$maxPages;
		if($maxPages==0)
			$curPage=1;
		if(isset($_REQUEST['curPage']))
		{
			$curPage=$_REQUEST['curPage'];
		}
		$start=($curPage-1)*$rowPerPage;
		$end=$start+$rowPerPage;
		
		$result=BinhLuanSuKienDAO::LayDanhSachBinhLuanTheoMaSK($mask,$start,$end);
		if($result!=null)
		{
			foreach($result as $s)
			{
				echo("<div class='comment-item'>
						<div class='avatar-col'>
							<img src='../images/$s->tk_avatar' />
						</div>
						<div class='comment-info-col'>
							<p class='username'><b>Người bình luận : </b><a href='#'>$s->blsk_username</a></p>
							<p class='date'>Ngày Đăng : $s->blsk_ngay_tao</p>
							<p class='comment1'>Nội dung : $s->blsk_noi_dung</p>
						</div>								
					</div>");
			}
			echo("
			<div class='comment-box'>
				<p class='comment-box-title'>Đăng bình luận</p>
				<textarea id='content-comment'></textarea>
				<div align='center'>
				<input class='buy btn' type='button' value='Bình luận' onClick='submitBinhLuan($mask);' />
				</div>
			</div>");
			if($maxItems>0)
			{
				$str="<div class='paper' style='float:left'><table><tr><td>Trang :";
				for($i=1;$i<=$maxPages;$i++)
				{
					if($i==$curPage)
					{
						$str=$str."<b id='curPage' title='$i' style='color:red'>$i&nbsp;&nbsp;</b>";
					}
					else
					{
						$str=$str."<label onclick='indexClick($i,$mask);' style='text-decoration:none;cursor:pointer;'>$i&nbsp;&nbsp;</label>";
					}
				}
				$str=$str."</tr></table></div>";
				$str=$str."<input type='hidden' value='$mask' id='mask' />";
				echo($str);
			}
		}
		else
		{
			echo("<h3>Chưa có bình luận nào hết</h3>");
			echo("
			<div class='comment-box'>
				<p class='comment-box-title'>Đăng bình luận</p>
				<textarea id='content-comment'></textarea>
				<div align='center'>
				<input class='buy btn' type='button' value='Bình luận' onClick='submitBinhLuan($mask);' />
				</div>
			</div>");
		}
	}
?>